// 获取页面尺寸，用于判断是移动端还是PC端
let pageWidth = document.body.clientWidth;
let verificationCodeBtn, registerBtn, passwordInput, weakLevel, middleLevel, strongLevel = "";
// 根据响应式布局分别获取数据
if (pageWidth >= 768) {
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[0];
    registerBtn = document.getElementsByClassName("registerBtn")[0];
    passwordInput = document.getElementsByClassName("password")[0];
    weakLevel = document.getElementsByClassName("weakLevel")[0];
    middleLevel = document.getElementsByClassName("middleLevel")[0];
    strongLevel = document.getElementsByClassName("strongLevel")[0];
} else {
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[1];
    registerBtn = document.getElementsByClassName("registerBtn")[1];
    passwordInput = document.getElementsByClassName("password")[1];
    weakLevel = document.getElementsByClassName("weakLevel")[1];
    middleLevel = document.getElementsByClassName("middleLevel")[1];
    strongLevel = document.getElementsByClassName("strongLevel")[1];
}

let verCode = 0;
let count = 60;
/*获取验证码*/
verificationCodeBtn.addEventListener('click', () => {
    let inputEmail = "";
    if (pageWidth >= 768) {
        inputEmail = document.getElementsByClassName("email")[0].value;
    } else {
        inputEmail = document.getElementsByClassName("email")[1].value;
    }
    if (inputEmail.length === 0) {
        alertBySweet("邮箱不能为空");
    } else if (inputEmail.indexOf("@") == -1 || inputEmail.indexOf(".com") == -1) {
        alertBySweet("请输入正确格式的邮箱！");
    } else {
        let timer = setInterval(function () {
            verificationCodeBtn.innerHTML = "剩余" + count + "s";
            count--;
            if (count === -1) {
                verificationCodeBtn.innerHTML = "获取验证码";
                clearInterval(timer);
                count = 60;
            }
        }, 1000);
        //给邮箱发送验证码
        if (count === 60) {// 60秒发送一次验证码
            ajax({
                type: 'POST',
                url: api.sendEmail,
                async: true,
                contentType: "application/json;charset=utf-8",
                data: {
                    email: inputEmail,
                },
                success: function (data) {
                    let res = JSON.parse(data);
                    if (res.statusCode === 200) {
                        alertBySweet("邮件发送成功，请查收！");
                        verCode = res.data.verCode;
                    } else {
                        alertBySweet("邮件发送失败，请稍后重试！");
                    }
                },
                error: function () {
                    alertBySweet("发送失败");
                }
            });
        }
    }
});

/*监听密码强弱*/
function watchPsw() {
    let pswValue = passwordInput.value;
    let number = /\d/.test(pswValue);
    let lowLetter = /[a-z]/.test(pswValue);
    let upperLetter = /[A-Z]/.test(pswValue);
    let special = /\W/.test(pswValue);
    if (special) {
        alertBySweet("只能输入数字与字母的组合！");
    }
    if (pswValue.length < 6 && ((lowLetter && !upperLetter) || (!lowLetter && upperLetter))) {
        //密码强度为弱
        weakLevel.style.backgroundColor = "red";
        middleLevel.style.backgroundColor = "#f6f6f6";
        strongLevel.style.backgroundColor = "#f6f6f6";
    } else if ((number && lowLetter && !upperLetter) ||
        (number && !lowLetter && upperLetter) ||
        (!number && lowLetter && upperLetter)) {//任意两种组合
        //密码强度为中
        weakLevel.style.backgroundColor = "orange";
        middleLevel.style.backgroundColor = "orange";
        strongLevel.style.backgroundColor = "f6f6f6";
    } else if ((number && lowLetter && upperLetter)) {
        //密码强度为强
        weakLevel.style.backgroundColor = "#85d6ff";
        middleLevel.style.backgroundColor = "#85d6ff";
        strongLevel.style.backgroundColor = "#85d6ff";
    }
}

/*监听注册按钮*/
registerBtn.addEventListener('click', function () {
    let inputVerCode, password, againPassword, inputEmail, username = "";
    if (pageWidth >= 768) {
        inputVerCode = document.getElementsByClassName("verCode")[0].value;
        password = document.getElementsByClassName("password")[0].value;
        againPassword = document.getElementsByClassName("againPassword")[0].value;
        inputEmail = document.getElementsByClassName("email")[0].value;
        username = document.getElementsByClassName("userName")[0].value;
    } else {
        inputVerCode = document.getElementsByClassName("verCode")[1].value;
        password = document.getElementsByClassName("password")[1].value;
        againPassword = document.getElementsByClassName("againPassword")[1].value;
        inputEmail = document.getElementsByClassName("email")[1].value;
        username = document.getElementsByClassName("userName")[1].value;
    }
    let lowLetter = /[a-z]/.test(password);
    let upperLetter = /[A-Z]/.test(password);
    if (!lowLetter && !upperLetter) {
        alertBySweet("密码必须是字母和数字的组合！");
        return;
    }
    if (password !== againPassword) {
        alertBySweet("两次密码输入的值不一样！");
        return;
    }

    if (inputVerCode === verCode) {//验证码正确
        //给邮箱发送验证码
        ajax({
            type: 'POST',
            url: api.userRegister,
            async: true,
            contentType: "application/json;charset=utf-8",
            data: {
                userName: username,
                email: inputEmail,
                password: password
            },
            success: function (data) {
                let res = JSON.parse(data);
                if (res.statusCode === 200) {
                    alertBySweet("注册成功！");
                    sessionStorage.setItem("userEmail", inputEmail);
                    sessionStorage.setItem("user_name", username);
                    location.href = route.noviceGuidance;
                } else {
                    alertBySweet("注册失败，请重试！");
                }
            },
            error: function () {
                alertBySweet("登录失败");
            }
        });
    } else {
        alertBySweet("验证码输入错误！");
    }
});
